﻿using System;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using QTMusic.BaseClasses;

namespace QTMusic.Websites
{
    public partial class ProfileEdit : System.Web.UI.Page
    {
        private string uploadDirectory = "";
        private string uploadFileName = "pic";
        private string serverPath = "";
        private int userId = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies["QTCookie"];
            if (cookie == null)
            {
                Response.Redirect("Login.aspx");
            }

            userId = Int32.Parse(cookie["ID"]);

            if (Request.QueryString["w"] == "image")
            {
                uploadDirectory = Path.Combine(Request.PhysicalApplicationPath, "ProfileImages");
                imgUploaded.Visible = true;
                uplImage.Visible = true;
                cmdUpload.Visible = true;
                lblResult.Visible = true;

                string selectSql = "SELECT * FROM [user] WHERE user_id = @userId";
                SqlConnection conn = new SqlConnection(Common.connString);
                SqlCommand cmd = new SqlCommand(selectSql, conn);
                cmd.Parameters.AddWithValue("@userId", userId);
                SqlDataReader reader;

                conn.Open();
                reader = cmd.ExecuteReader();
                reader.Read();

                if (!reader.HasRows)
                {
                    Response.Redirect("Login.aspx");
                }

                imgUploaded.ImageUrl = (string)reader["user_pic"];
                uploadFileName += reader["user_id"].ToString();
                conn.Close();
            }
        }

        public void cmdUpload_Click(object sender, EventArgs e)
        {
            if (uplImage.PostedFile.FileName == "")
            {
                lblResult.Text = "Hãy chọn một bức ảnh.";
            }
            else
            {
                string extension = Path.GetExtension(uplImage.PostedFile.FileName);
                switch (extension.ToLower())
                {
                    case ".jpg":
                    case ".png":
                    case ".jpeg":
                        break;
                    default:
                        lblResult.Text = "Định dạng này không được cho phép.";
                        return;
                }

                uploadFileName += extension;
                string fullUploadPath = Path.Combine(uploadDirectory, uploadFileName);
                serverPath = Common.urlProfileImages + uploadFileName;

                try
                {
                    uplImage.PostedFile.SaveAs(fullUploadPath);
                    Common.UpdateProfilePic(userId, serverPath);
                    lblResult.Text = "Ảnh đại diện đã được thay đổi.";
                    imgUploaded.ImageUrl = serverPath;
                }
                catch (Exception profileEditEx)
                {
                    Common.WriteToEventLog(profileEditEx, "UploadProfilePicture");
                    lblResult.Style["color"] = "Red";
                    lblResult.Text = "Xảy ra sự cố. Xin hãy thử lại.";
                }
            }
        }
    }
}